package com.crm.service.impl.opportunityManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.opportunityManager.OpportunityGiftMapper;
import com.crm.model.entity.opportunityManager.OpportunityGift;
import com.crm.model.entity.opportunityManager.OpportunityGiftExample;
import com.crm.model.entity.opportunityManager.OpportunityProduct;
import com.crm.model.entity.opportunityManager.OpportunityProductExample;
import com.crm.service.opportunityManager.OpportunityGiftDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商机赠品
 */
@Service
public class OpportunityGiftDbServiceImpl implements OpportunityGiftDbService {

    @Autowired
    private OpportunityGiftMapper opportunityGiftMapper;

    /**
     * 商机-新增商机-查询商机下的赠品
     * @param opportunityId 商机id
     * @param order 分组
     * @return
     */
    @Override
    public List<OpportunityGift> opportunityGiftList(String opportunityId, String order) {
        OpportunityGiftExample opportunityGiftExample = new OpportunityGiftExample();
        OpportunityGiftExample.Criteria criteria = opportunityGiftExample.createCriteria();
        opportunityGiftExample.setOrderByClause(order);
        criteria.andOpportunityIdEqualTo(opportunityId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityGift> list = opportunityGiftMapper.selectByExample(opportunityGiftExample);
        return list;
    }

    /**
     * 商机管理-新增商机赠品
     * @param opportunityGift 商机赠品实体
     * @return
     */
    @Override
    public Boolean addOpportunityGift(OpportunityGift opportunityGift) {
        Boolean result = false;
        int num = opportunityGiftMapper.insertSelective(opportunityGift);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 商机-商机详情-修改商机赠品回显
     * @param opportunityGiftId 商机赠品id
     * @return
     */
    @Override
    public OpportunityGift getOpportunityGiftById(String opportunityGiftId) {
        return opportunityGiftMapper.selectByPrimaryKey(opportunityGiftId);
    }

    /**
     * 商机管理-修改商机赠品
     * @param opportunityGift 商机赠品实体
     * @return
     */
    @Override
    public Boolean updateOpportunityGift(OpportunityGift opportunityGift) {
        Boolean result = false;
        int num = opportunityGiftMapper.updateByPrimaryKeySelective(opportunityGift);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 商机详情-赠品查重
     * @param giftCode 赠品编码
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public List<OpportunityGift> opportunityCodeAndOppIdList(String giftCode, String opportunityId) {
        OpportunityGiftExample opportunityGiftExample = new OpportunityGiftExample();
        OpportunityGiftExample.Criteria criteria = opportunityGiftExample.createCriteria();
        criteria.andGiftCodeEqualTo(giftCode);
        criteria.andOpportunityIdEqualTo(opportunityId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityGift> list = opportunityGiftMapper.selectByExample(opportunityGiftExample);
        return list;
    }
}
